<template>
	<view class="login">
		<uni-status-bar />
		<view class="navbar">登录</view>
		<view class="form form-a">
			<view class="form-group">
				<view class="form-item">
					<view class="form-item-body">
						<input type="text" class="form-control" placeholder="用户名" v-model="userName" />
					</view>
				</view>
				<view class="form-item">
					<view class="form-item-body">
						<input type="password" class="form-control" placeholder="密码" v-model="password" />
					</view>
				</view>
				
				<view class="form-item form-item-code">
					<view class="form-item-body">
						<input type="text" class="form-control" placeholder="请输入验证码" v-model="code" />
					</view>
					<view class="form-item-right">
						<image class="code" mode="aspectFit" :src="codeImage" @tap="bindGetCode"></image>
					</view>
				</view>
			</view>
			
			<view class="btns">
				<view class="btns-group">
					<button class="btn" @tap="submit">登录</button>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import { login, getCaptchaImage } from '@/api/configure.js';
	import { deepClone} from '@/util/util.js';

	export default {
		data() {
			return {
				userName: '',
				password: '',
				codeImage: '',
				uuid: '',
				code: ''
			}
		},
		methods: {
			getCode() {
				getCaptchaImage().then((res) => {
					this.codeImage = `data:image/gif;base64,${res.img}`;
					this.uuid = res.uuid;
				}, err => {})
			},
			bindGetCode() {
				this.getCode()
			},
			async init() {
				this.getCode()
			},
			async submit() {
				if (!this.userName) {
					uni.showToast({
						title: '用户名不能为空',
						duration: 2000,
						icon: 'none'
					});

					return
				}

				if (!this.password) {
					uni.showToast({
						title: '密码不能为空',
						duration: 2000,
						icon: 'none'
					});

					return
				}
				
				if (!this.code) {
					uni.showToast({
						title: '验证码不能为空',
						duration: 2000,
						icon: 'none'
					});
				
					return
				}
				
				let data = {
					"code": this.code,
					"password": this.password,
					"username": this.userName,
					"uuid": this.uuid
				}

				this.$store.dispatch({
					type: 'login',
					data: data
				}).then(() => {
					this.openPage();
				},() => {
					this.getCode();
				})
			},
			openPage() {
				uni.navigateTo({
					url: '/pages/index/index'
				});
			}
		},
		onLoad() {},
		onReady() {
			let userName = uni.getStorageSync('userName');
			let password = uni.getStorageSync('password');

			if (userName && password) {
				this.userName = userName;
				this.password = password;
			} else {
				this.userName = '';
				this.password = '';
			}
			
			this.init();
		},
	}
</script>

<style lang="scss">
	.login{
		background: #eff3f9 url('~@/static/icon/login_bg.png') 50% 0% no-repeat;
		background-size: 100% auto;
		position: fixed;
		top: 0px;
		right: 0px;
		bottom: 0px;
		left: 0px;
	}
	
	.navbar{
		padding: 100rpx 30rpx 69rpx;
		font-size: 48rpx;
		color: #ffffff;
		line-height: 58rpx;
	}
	.form {
		margin: 0px 30rpx 0rpx;
		padding: 110rpx 0rpx 0rpx 0rpx;
		height: 970rpx;
		border-radius: 35rpx;
		background-color: #ffffff;
		.form-group{
			padding: 0px 43rpx;
			.form-item{
				padding: 35rpx 22rpx;
				margin-bottom: 8rpx;
				display: flex;
				position: relative;
				top: 0px;
				left: 0px;
				&:after{
					position: absolute;
					content: '';
					bottom: 0rpx;
					right: 0rpx;
					left: 0rpx;
					height: 1rpx;
					background-color: #dddddd;
				}
				.form-item-body{
					width: 100%;
					.form-control{
						height: 65rpx;
						font-size: 30rpx;
						line-height: 36rpx;
					}
				}
			}
			.form-item-code{
				.form-item-body{
					width: calc(100% - 137rpx);
				}
				.form-item-right{
					width: 137rpx;
					height: 65rpx;
					.code{
						width: 100%;
						height: 100%;
					}
				}
			}
		}
	}

	.btns {
		margin-top: 118rpx;
		.btns-group {
			padding: 0rpx 60rpx;
		}
	}

	.btn {
		padding: 29rpx 0px;
		background-image: linear-gradient(to bottom right, #45b3ee, #3a8ded);
		font-size: 36rpx;
		line-height: 42rpx;
		color: #ffffff;
		border-radius: 50rpx;
	}
</style>
